package cn.yeziji.forum.converter;

import cn.yeziji.forum.dto.pay.merchant.MerchantWechatPayInfoDTO;
import cn.yeziji.forum.entity.MerchantWechatPayInfoEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * 支付对象转换器
 *
 * @author gzkemays
 * @since 2023/07/25 23:19
 **/
@Mapper
public interface PayConverter {

  PayConverter INSTANCE = Mappers.getMapper(PayConverter.class);

  String PAY_ENCRYPT_SALT = "xI_qmt5AKeo2Cd1oYxD92wiyXp75AJ@yzj.pay";

  /**
   * 商户微信支付信息 数据对象 转换为对应的实体类
   *
   * @param infoDTO 商户微信支付信息 数据对象
   * @return {@link MerchantWechatPayInfoDTO} 商户微信支付信息实体类
   */
  @Mapping(target = "userId", expression = "java(cn.yeziji.forum.utils.UserOnlineUtils.getUserId())")
  @Mapping(target = "appId", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getAppId(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "merchantLoginUsername", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getMerchantLoginUsername(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "merchantNumber", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getMerchantNumber(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "apiKey", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getApiKey(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "apiKeyV3", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getApiKeyV3(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "appSecurity", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getAppSecurity(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "domain", expression = "java(cn.yeziji.forum.utils.AesUtils.encrypt(infoDTO.getDomain(), PayConverter.PAY_ENCRYPT_SALT))")
  MerchantWechatPayInfoEntity merchantWechatPayInfoDTOConvertToEntity(MerchantWechatPayInfoDTO infoDTO);

  /**
   * entity 转数据对象
   *
   * @param entity 实体类
   * @return {@link MerchantWechatPayInfoDTO} 微信商户数据对象
   */
  @Mapping(target = "appId", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getAppId(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "merchantLoginUsername", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getMerchantLoginUsername(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "merchantNumber", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getMerchantNumber(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "apiKey", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getApiKey(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "apiKeyV3", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getApiKeyV3(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "appSecurity", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getAppSecurity(), PayConverter.PAY_ENCRYPT_SALT))")
  @Mapping(target = "domain", expression = "java(cn.yeziji.forum.utils.AesUtils.decrypt(entity.getDomain(), PayConverter.PAY_ENCRYPT_SALT))")
  MerchantWechatPayInfoDTO merchantWechatPayInfoEntityConvertToDTO(MerchantWechatPayInfoEntity entity);
}
